<template>
  <div class="app-container">
    <basic-query-form ref="queryForm" @doQuery="getList"  @resetQuery="getList" :dept-type="2" :no-programme="true">
      <template v-slot:queryItem="data">
        <el-form-item label="考核码" >
          <el-input
            v-model="data.queryParams.assessmentCode"
            placeholder="请输入考核码"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="职位" >
          <el-select v-model="data.queryParams.postCode" placeholder="请选择" clearable size="small">
            <el-option  v-for="dict in positionOptions"
                        :label="dict.name"
                        :key="dict.value"
                        :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </template>
    </basic-query-form>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['kh:code:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kh:code:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="codeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="id" align="center" prop="id" />-->
      <el-table-column label="考核码" align="center" prop="assessmentCode" />
      <el-table-column label="所属用户" align="center" prop="showName" />
      <el-table-column label="所属部门" align="center" prop="deptName" />
      <el-table-column label="职位" align="center" prop="postCode" :formatter="positionFormat"/>

      <el-table-column label="考核名称" align="center" prop="assessmentName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['kh:code:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kh:code:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getListByQueryForm"
    />

    <!-- 添加或修改考核码对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="考核码" prop="assessmentCode">
          <el-input v-model="form.assessmentCode" placeholder="请输入考核码" />
        </el-form-item>
        <el-form-item label="一对一的用户 (kh_user)" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入一对一的用户 (kh_user)" />
        </el-form-item>
        <el-form-item label="0。正职" prop="level">
          <el-input v-model="form.level" placeholder="请输入0。正职" />
        </el-form-item>
        <el-form-item label="考核关联" prop="assessmentId">
          <el-input v-model="form.assessmentId" placeholder="请输入考核关联" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCode, getCode, delCode, addCode, updateCode, exportCode } from "@/api/kh/code";
import BasicQueryForm from '../../components/BasicQueryForm'
import { optionsPost } from  "@/api/system/post";
export default {
  name: "Code",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      //分公司 集团 集合
      deptList: [],
      //考核列表
      AssessmentOptions:[],
      //职位字典集合
      positionOptions: [],
      // 考核码表格数据
      codeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  components: {
    'BasicQueryForm' : BasicQueryForm
  },
  created() {
    optionsPost().then(response => {
      this.positionOptions = response.data;
    });

  },
  methods: {
    getListByQueryForm(){
      this.$refs.queryForm.pageQuery(this.queryParams)
    },
    //职位格式翻译
    positionFormat(row,column){
      return this.selectOptionName(this.positionOptions,row.postCode)
    },
    /** 查询考核码列表 */
    getList(queryParams) {
      this.queryParams.pageNum = queryParams.pageNum;
      this.queryParams.pageSize = queryParams.pageSize;
      this.loading = true;
      listCode(queryParams).then(response => {
        this.codeList = response.rows;
        this.total = response.total;
        this.AssessmentOptions = this.$refs.queryForm.AssessmentOptions;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        assessmentCode: undefined,
        userId: undefined,
        level: undefined,
        assessmentId: undefined
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考核码";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCode(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考核码";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCode(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getListByQueryForm();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addCode(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getListByQueryForm();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let rowsDeleted = this.$CrudUtils.rowsDeleted(
        row, this.ids, this.codeList, o=>o.id, o=>o.assessmentCode
      )
      this.$confirm('是否确认删除考核码[ "' + rowsDeleted.msgs + ' ]', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCode(rowsDeleted.ids);
        }).then(() => {
          this.getListByQueryForm();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有考核码数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCode(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
